package com.bs.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.bs.product.entity.Type;
import com.bs.product.service.ITypeService;
import com.bs.product.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lukang
 * @since 2022-11-05
 */
@RestController
@RequestMapping("/type")
@Api("商品分类Controller")
public class TypeController {
    @Autowired
    ITypeService service;
    //查询所有
    @ApiOperation("查询所有")
    //@GetMapping("list")
    @GetMapping
   public AjaxResult getAll(){
        //指定排序列
        LambdaQueryWrapper<Type> qw= new LambdaQueryWrapper<>();
        //qw.orderBy(true,false,Type::getSort,Type::getId);
        qw.orderByAsc(Type::getSort,Type::getId);
        List<Type> list = service.list(qw);
        return  AjaxResult.success(list);
    }

    //添加
    @ApiOperation("添加")
   // @PostMapping( "add")
    @PostMapping
    public AjaxResult add(@ApiParam("视频分类对象")Type type){
//        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String datestr = sf.format(new Date());
//        type.setCreateTime(datestr);
        boolean save = service.save(type);
        if (save){
            return  AjaxResult.success("分类添加成功");
        }else {
            return AjaxResult.fail("分类添加失败");
        }
    }

    //根据id查询
    @ApiOperation("根据id查询对象")
    //@GetMapping("get/{id}")
    @GetMapping("{id}")
    public AjaxResult get(@ApiParam("视频分类ID")@PathVariable("id") long id){
      return AjaxResult.success(service.getById(id));
    }

    //修改
    @ApiOperation("修改")
    // @PutMapping("update")
    @PutMapping
    public AjaxResult update(@ApiParam("视频分类对象")Type type){
//        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String datestr = sf.format(new Date());
//        type.setCreateTime(datestr);
        boolean update = service.updateById(type);
        if (update){
            return  AjaxResult.success("修改成功");
        }else {
            return AjaxResult.fail("修改失败");
        }
    }

    //删除
    @ApiOperation("删除")
    //@DeleteMapping("/del")
    @DeleteMapping("/{id}")
    public AjaxResult del(@ApiParam("视频分类ID") @PathVariable("id") long id){
       // boolean isdel = service.removeById(id);
        LambdaUpdateWrapper<Type> wrapper = new LambdaUpdateWrapper<>();
       wrapper.eq(Type::getId,id).set(Type::getDeleted,1);

        boolean isdel = service.update(wrapper);
        if (isdel){
            return  AjaxResult.success("删除成功");
        }else {
            return AjaxResult.fail("删除失败");
        }

    }
}

